/*************************************************************************
 *
 * Obsluha seriove komunikace - SPOLECNE FUNKCE
 * includovano z serial.h souboru projektu
 *   
 * $Id: serial.h 119 2007-01-22 17:35:57Z Ales $
 * 
 *************************************************************************/         

#define PACKET_PREFIX           0xaa

#define PACKET_DATA_LENGTH      64

#define PACKET_TX_BUF_SIZE      PACKET_DATA_LENGTH
#define PACKET_RX_BUF_SIZE      (PACKET_DATA_LENGTH - (sizeof(PACKET_HEADER) + sizeof(PACKET_FOOTER) + 3))
#define PACKET_TX_MAX_LENGTH    (PACKET_TX_BUF_SIZE - (sizeof(PACKET_HEADER) + sizeof(PACKET_FOOTER) + 3))
#define PACKET_RX_MAX_LENGTH    (PACKET_RX_BUF_SIZE + sizeof(PACKET_HEADER))

#define PACKET_RX_TIMEOUT       100
#define PACKET_TX_TRIES         10

#define PACKET_RAND_DELAY       ((rand() & 0x03ff) | 0x003f)


typedef union {
    struct {
        unsigned char seq;
        unsigned char dest;
        unsigned char src;
        unsigned char cmd;
        union {
            unsigned int item;
            struct {
                unsigned char item_hi;
                unsigned char item_lo;
            };
        };
    };
    unsigned char raw_data[0];
} PACKET_HEADER;


typedef union {
    unsigned int crc;
    unsigned char raw_data[0];
} PACKET_FOOTER;


#define ITEM_ALL                255

#define CMD_ACKNOWLEDGE         10
#define CMD_BOOTLOADER          20
#define CMD_DEBUG               30

#define CMD_DESC_WRITE          100
#define CMD_DESC_READ           101
 #define ITEM_SENSOR_COUNT      10
 #define ITEM_SENSOR_DESC       11
 #define ITEM_MODE_COUNT        20
 #define ITEM_MODE_DESC         21

#define CMD_STDATA_WRITE        110
#define CMD_STDATA_READ         111
 #define ITEM_TEMPERATURE       10
 #define ITEM_STATE             20
 #define ITEM_MESSAGE           100 // formalne patri pod STDATA, ale neni clenem struktury
//define ITEM_ALL               255

#define CMD_RWDATA_WRITE        120
#define CMD_RWDATA_READ         121
 #define ITEM_MODE              10
 #define ITEM_SHIFT             20
 #define ITEM_SWITCH            30
 #define ITEM_RTC               100 // formalne patri pod RWDATA, ale neni clenem struktury
//define ITEM_ALL               255

#define CMD_LOG_WRITE           130
#define CMD_LOG_READ            131
#define CMD_LOG_ERASE           132

#define CMD_ONEWIRE_SCAN        140

#define CMD_INIT_WRITE          150
#define CMD_INIT_READ           151

#define ADDR_SERVER             0
#define ADDR_BROADCAST          255

typedef struct PROGMEM
{
    unsigned char cmd;
    void (*pHandlerFunc)(PACKET_HEADER *ph, char *data, unsigned char length);
} SERIAL_HANDLER;


AVRX_EXTERNTASK(SerialTask);
extern void InitSerialTask(void);

extern void SerialSendAck(PACKET_HEADER *ph);
extern void SerialSendPacket(PACKET_HEADER *ph, char *data, unsigned char length, char gen_seq);

extern int SerialWaitAck(unsigned char seq);
